package pikes.peak;

import java.util.HashMap;
import java.util.Locale;

import org.custommonkey.xmlunit.Diff;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.StaticMessageSource;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.support.GenericWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.i18n.FixedLocaleResolver;
import org.springframework.web.servlet.theme.FixedThemeResolver;
import org.springframework.web.util.WebUtils;

import pikes.peak.test.MockTheme;
import pikes.peak.test.MockThemeSource;
import pikes.peak.test.PikesPeakComponentTestCase;


public class PikesPageTestCase extends PikesPeakComponentTestCase {

	@Test @Ignore
	public void simplePage() throws Exception {
		PikesPeakPage ppp = new PikesPeakPage() {

			@Override
			protected void buildPage(Page page) {
				
			}
			
		};
		

		FixedLocaleResolver localeResolver = new FixedLocaleResolver();
		localeResolver.setDefaultLocale(Locale.ENGLISH);

		FixedThemeResolver themeResolver = new FixedThemeResolver();
		themeResolver.setDefaultThemeName("myTheme");
		
		StaticMessageSource messageSource = new StaticMessageSource();
		
		StaticMessageSource themeMessageSource = new StaticMessageSource();
		
		DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
		beanFactory.registerSingleton(AbstractApplicationContext.MESSAGE_SOURCE_BEAN_NAME, messageSource);
		beanFactory.registerSingleton("testPage", ppp);
		
		
		MockThemeSource themeSource = new MockThemeSource();
		themeSource.addTheme(new MockTheme("myTheme", themeMessageSource));

		beanFactory.registerSingleton("themeSource", themeSource);
		
		MockServletContext servletContext = new MockServletContext();
		servletContext.addInitParameter(WebUtils.HTML_ESCAPE_CONTEXT_PARAM, Boolean.FALSE.toString());
		
		GenericWebApplicationContext webApplicationContext = new GenericWebApplicationContext(beanFactory);
		webApplicationContext.setServletContext(servletContext);
		webApplicationContext.refresh();
		ppp.setApplicationContext(webApplicationContext);
		
		MockHttpServletRequest request = new MockHttpServletRequest(servletContext, "GET", "/countries");
		request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, webApplicationContext);
		request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, themeResolver);
		request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, localeResolver);
		request.setAttribute(DispatcherServlet.THEME_SOURCE_ATTRIBUTE, webApplicationContext);
		request.setContextPath("/countries");
		MockHttpServletResponse response = new MockHttpServletResponse();
		ppp.render(new HashMap(), request, response);
		
		

		Diff diff = new Diff("<?xml version=\"1.0\"?><!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"><html xmlns=\"http://www.w3.org/1999/xhtml\"><head><title></title><meta content=\"text/html;charset=ISO-8859-1\" http-equiv=\"Content-Type\"></meta></head><body></body></html>", response.getContentAsString());

		Assert.assertTrue(diff.toString(),diff.identical());
		
	}
	
	@Test
	public void pageWithEmptyTitle() throws Exception {
		Page page = new Page();
		Assert.assertEquals("",page.getTitle().getValue(new HashMap(), createPikesPeakTestRequest().getRequestContext()));
	}
	
	@Test
	public void pageWithNotEmptyTitle() throws Exception {
		Page page = new Page();
		page.setTitle("Hello");
		Assert.assertEquals("Hello",page.getTitle().getValue(new HashMap(), createPikesPeakTestRequest().getRequestContext()));
	}
	
	@Test
	public void pageWithTranslatedTitle() throws Exception {
		Page page = new Page();
		page.setTitle("Some text");
		Assert.assertEquals("Translated Text",page.getTitle().getValue(new HashMap(), createPikesPeakTestRequest().getRequestContext()));
	}
	
}
 